;$(function () {
    var serverIdArr =[];
    var id = $("#id").val();
    if(id==null||id==""){
    	getDeptList();
    	getPrmissList(null);
    }else{
        $("#username").attr("disabled","disabled");
    	$("#dept_select").attr("disabled","disabled");
    	$("#user_select").attr("disabled","disabled");
    	$("#tip").show();
    	getInfo(id, serverIdArr);
    	getPrmissList(id);
    }
    submitForm();
})
var tale = new $.tale();//弹框

function getInfo(id, serverIdArr) {
    $.ajax({
        url: ctxPath+"/set/admin/findById/" + id,
        type: 'GET',
        success: function (d) {
            var success = d.success;
            if (success == true) {
                var data = d.data;
                umbinding.bind(data);
                $("#dept_select").html('<option value="'+data.deptUser.deptId+'" check>'+data.deptUser.deptName+'</option>');    
                $("#user_select").html('<option  check>'+data.nickname+'</option>');    
                $("#user_select").html('<option  check>'+data.nickname+'</option>');    
                $("#user_select").html('<option  check>'+data.nickname+'</option>');  
            }
        },
        error: function () {
        }

    });
}


function submitForm() {
    $("#global-form").validator({
        rules: {
            mobile: function (element, params) {
                var tel = /^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
                return tel.test(element.value) || '请检查电话号码';
            },
            username:function(element){
	    		var name = element.name;
	    		var json={};
	    		   json[name] = element.value;
	    		  var value = $("#id").val();
	    		  if(value!=null&&value!=''){
	    			  var id="id";
	    			  json[id] = value;
	    		  }
	    		var isValid = true;
	        	$.ajax({
	        		 url: ctxPath+'/set/admin/checkUsername',
	                 type: 'post',
	                 data: json,
	                 dataType: 'json',
	                 async:false,
	                 success: function(d){
	                	 isValid =  d;
	                 }
	        	})
	        	return isValid|| "帐号重复";
	    	},
	    	isPwd:function(element){
	    		 var isInvalid = false;
	    		 var value = $("#id").val();
	    		 if(value==null||value==''){
	    			 isInvalid =true;
	    		 }
	    		 return isInvalid|| "必填";
	    	}
        },
        fields: {
            username: {
                rule: "required;username",
                msg: {
                    required: "必填!"
                }
            },
            userId: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            password: {
                rule: "required(isPwd);",
                msg: {
                    required: "必填!"
                }
            },
            repassword: {
                rule: "required(isPwd);密码:match(password)",
                msg: {
                    required: "必填!"
                }
            },
        },
        invalid: function (form, errors) {
            console.log(errors);
            $(".btn-submit").removeAttr("style");
        },
        //验证成功
        valid: function (form) {
            saveInfo();
        },
    });
}

function saveInfo() {
    var json = $("#global-form").serializeFormJSON();
    console.log(json);
    var id = $("#id").val();
    var url = ctxPath+"/set/admin/saveInfo";
    if (id != '' && id != null) {
        url = ctxPath+"/set/admin/updateInfo";
    }
    $.ajax({
        url: url,
        type: 'POST',
        data: json,
        success: function (d) {
            var success = d.success;
            if (success) {
                tale.alertOk({
                    text: '保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = ctxPath+'/set/admin/list';
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.message || '保存失败');
            }
        }
    });
}

/**
 * 获取组织机构
 * @returns
 */
function getDeptList(){
	$.ajax({
        url: ctxPath+"/agency/dept/getAllList",
        type: 'GET',
        success: function(d){
        	var success = d.success;
        	 if(success==true){
        	if(d.data!=null){
        		var result ='<option value="">请选择</option>';
        		var deptId="";
        	$.each(d.data,function(i,item){
//        		if(i==0){
//        			deptId=item.id;
//        		}
        		result+='<option value="'+item.id+'">'+item.name+'</option>';	
        	})
        	$("#dept_select").html(result);
//        	getUserList(deptId);
        	}	 
        	}
        	 
	}
   })
}


/**
 * 获取机构下的人员
 * @returns
 */
function getUserList(deptId){
	var result ='<option value="">请选择</option>';
	if(deptId==null||deptId==''){
		$("#user_select").html(result);
		return;
	}
	$.ajax({
		url: ctxPath+"/agency/user/findByDeptId/"+deptId,
		type: 'GET',
		success: function(d){
			var success = d.success;
			if(success==true){
				if(d.data!=null){
					$.each(d.data,function(i,item){
						result+='<option value="'+item.userId+'">'+item.userName+'</option>';	
					})
					$("#user_select").html(result);
				}	 
			}
			
		}
	})
}


/**
 * 获取权限
 * @returns
 */

function getPrmissList(userId){
	$.ajax({
		url: ctxPath+"/set/permission/getPermission",
		data:{userId:userId},
		type: 'POST',
		success: function(d){
			var success = d.success;
			if(success==true){
				if(d.data!=null){
					var result ='';
					var type = "1";
					$.each(d.data,function(i,item){
						if(item.type!=type){
							result+="<br><br>";
							type=item.type;
						}
						if(item.isCheck){
						 result+='<label class="checkbox-inline"><input type="checkbox"  value="'+item.id+'" name="permissionList['+i+'].permissionId" checked>'+item.menuName+'</label>';	
						}else{
							result+='<label class="checkbox-inline"><input type="checkbox"  value="'+item.id+'" name="permissionList['+i+'].permissionId">'+item.menuName+'</label>';	
						}
					})
					$("#modular").html(result);
				}	 
			}
			
		}
	})
}
/*
function getPrmissList(){
	$.ajax({
		url: "/set/permission/getPermission",
		type: 'GET',
		success: function(d){
			var success = d.success;
			if(success==true){
				if(d.data!=null){
					var result ='';
					var type = "1";
					$.each(d.data,function(i,item){
						console.log(item.type!=type); 
						console.log(item.type); 
						if(item.type!=type){
							result+="<br><br>";
							type=item.type;
						}
						if(item.isCheck){
						 result+='<label class="checkbox-inline"><input type="checkbox"  value="'+item.id+'" name="permissionList['+i+'].permissionId" checked>'+item.menuName+'</label>';	
						}else{
							result+='<label class="checkbox-inline"><input type="checkbox"  value="'+item.id+'" name="permissionList['+i+'].permissionId">'+item.menuName+'</label>';	
						}
					})
					$("#modular").html(result);
				}	 
			}
			
		}
	})
}*/

